package com.zy.web.servlet;

import com.zy.pojo.Department;
import com.zy.pojo.Employee;
import com.zy.pojo.ResultInfo;
import com.zy.service.DepartmentService;
import com.zy.service.impl.DepartmentServiceImpl;
import com.zy.web.servlet.base.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021/6/5
 * Time: 下午 13:46
 * Description: 部门控制器
 */
@WebServlet("/department/*")
public class DepartmentServlet extends BaseServlet {
    private DepartmentService departmentService = new DepartmentServiceImpl();

    public void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer page = null;
        Integer limit = null;
        try {
            page = Integer.parseInt(request.getParameter("page"));
            limit = Integer.parseInt(request.getParameter("limit"));
        } catch (NumberFormatException e) {
        }
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Department> departmentList = departmentService.getDepartmentList(page, limit);
        Integer count = departmentService.getDepartmentCount();
        map.put("code", 0);
        map.put("message", "操作成功");
        map.put("count", count);
        map.put("data", departmentList);
        writeValue(map, response);
    }

    public void getDepById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = null;
        try {
            id = Integer.parseInt(request.getParameter("id"));
        } catch (NumberFormatException e) {
            id = null;
            System.out.println("添加部门");
        }
        String departmentName = request.getParameter("departmentName");
        Department department = new Department();
        department.setId(id);
        department.setDepartmentName(departmentName);
        boolean result = false;
        if (id != null) {
            result = departmentService.updateDepartment(department);
        } else {
            result = departmentService.insertDepartment(department);
        }
        ResultInfo resultInfo = new ResultInfo();
        if (result) {
            resultInfo.setCode(200);
            resultInfo.setMessage("操作成功");
        } else {
            resultInfo.setCode(400);
            resultInfo.setMessage("操作失败");
        }
        writeValue(resultInfo, response);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Boolean result = departmentService.deleteDepartmentById(id);
        ResultInfo resultInfo = new ResultInfo();
        if (result) {
            resultInfo.setCode(200);
            resultInfo.setMessage("删除成功");
        } else {
            resultInfo.setCode(400);
            resultInfo.setMessage("该部门中还有员工不能删除");
        }
        writeValue(resultInfo, response);
    }

    public void deleteList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ids = request.getParameter("ids");
        String[] split = ids.split(",");
        ArrayList<String> msg = new ArrayList<>();
        for (String temp : split) {
            Boolean itemResult = departmentService.deleteDepartmentById(Integer.parseInt(temp));
            if (!itemResult) {
                msg.add(temp);
            }
        }
        ResultInfo resultInfo = new ResultInfo();
        if (msg.size() == 0) {
            resultInfo.setCode(200);
            resultInfo.setMessage("删除成功");
        } else {
            resultInfo.setCode(400);
            String returnMsg = "部门编号为：";
            for (String temp : msg) {
                returnMsg += temp + " ";
            }
            returnMsg += "的部门里还有员工，删除失败";
            resultInfo.setMessage(returnMsg);
        }
        writeValue(resultInfo, response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}